

class MyBlockingQueue{
    private int[]data=new int[1000];
    private int size;
    private int head;
    private int tail;
    private Object locker=new Object();
    public void put(int value) throws InterruptedException {
        synchronized (locker){
            if(size== data.length){
                locker.wait();
            }
            data[tail]=value;
            tail++;
            if(tail>= data.length){
                tail=0;
            }
            size++;
            locker.notify();
        }
    }
    public int take() throws InterruptedException {
        synchronized (locker){
            if(size==0){
                locker.wait();
            }
            int ret=data[head];
            head++;
            if(head>=data.length){
                head=0;
            }
            size--;
            locker.notify();
            return ret;
        }
    }
}
public class Demo1 {
    public static void main(String[] args) {
        MyBlockingQueue queue=new MyBlockingQueue();
        Thread producer=new Thread(()->{
            int num=0;
            while(true){
                System.out.println("生产了："+num);
                try {
                    queue.put(num);
                    num++;
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        producer.start();
        Thread customer=new Thread(()->{
            while(true){
                try {
                    int num=queue.take();
                    System.out.println("消费了:"+num);
                    Thread.sleep(500);

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        customer.start();
    }
}
